﻿/*
 * The MIT License (MIT)
 * 
 * Copyright (c) 2012 Junhua
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 * documentation files (the "Software"), to deal in the Software without restriction, including without 
 * limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
 * Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions
 * of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 * TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 * DEALINGS IN THE SOFTWARE.
 */

namespace ScheduleQuene.Tests
{
    using Microsoft.VisualStudio.TestTools.UnitTesting;
    using Schedule;

    [TestClass]
    public class SerialQueneTests
    {
        IScheduleQuene quene;

        [TestInitialize]
        public void Setup()
        {
            quene = new SerialQuene();
        }

        [TestMethod]
        public void TestSchedule100()
        {
            StaticList.List.Clear();

            int count = 0;
            ITask t1 = new TestTask();
            ITask t2 = new Test700Task();
            ITask t3 = new Test200Task();
            ITask t4 = new Test1200Task();

            t3.Completed += (sender, e) =>
            {
                count = StaticList.List.Count;
            };

            quene.Schedule(t1); // 500
            quene.Schedule(t2); // 700
            quene.Schedule(t3); // 200
            quene.Schedule(t4); // 1200
            System.Threading.Thread.Sleep(100);
            quene.Schedule(t2); // 700
            quene.Schedule(t4); // 1200

            System.Threading.Thread.Sleep(10000);

            Assert.AreEqual(4, count);
            Assert.AreEqual("TestTask - 500", StaticList.List[0]);
            Assert.AreEqual("Test1200Task - 1200", StaticList.List[1]);
            Assert.AreEqual("Test700Task - 700", StaticList.List[2]);
            Assert.AreEqual("Test200Task - 200", StaticList.List[3]);
        }

        [TestMethod]
        public void TestSchedule600()
        {
            StaticList.List.Clear();

            int count = 0;
            ITask t1 = new TestTask();
            ITask t2 = new Test700Task();
            ITask t3 = new Test200Task();
            ITask t4 = new Test1200Task();

            t3.Completed += (sender, e) =>
            {
                count = StaticList.List.Count;
            };

            quene.Schedule(t1); // 500 1
            quene.Schedule(t2); // 700 2
            quene.Schedule(t3); // 200 3
            quene.Schedule(t4); // 1200 4
            System.Threading.Thread.Sleep(1200);
            quene.Schedule(t4); // 1200 5
            quene.Schedule(t2); // 700 6

            System.Threading.Thread.Sleep(10000);

            Assert.AreEqual(4, count);
            Assert.AreEqual("TestTask - 500", StaticList.List[0]);
            Assert.AreEqual("Test1200Task - 1200", StaticList.List[1]);
            Assert.AreEqual("Test700Task - 700", StaticList.List[2]);
            Assert.AreEqual("Test200Task - 200", StaticList.List[3]);
        }

        [TestMethod]
        public void TestScheduleException()
        {
            StaticList.List.Clear();

            int count = 0;
            ITask t1 = new TestTask();
            ITask t2 = new Test700Task();
            ITask t3 = new Test200Task();
            ITask t4 = new Test1200Task();
            ITask exceptionTask = new ExceptionTask();

            t3.Completed += (sender, e) =>
            {
                count = StaticList.List.Count;
            };

            quene.Schedule(t1); // 500 1
            quene.Schedule(t2); // 700 2
            quene.Schedule(t3); // 200 3
            quene.Schedule(exceptionTask); // 100 4
            quene.Schedule(t4); // 1200 5
            System.Threading.Thread.Sleep(1200);
            quene.Schedule(t4); // 1200 6
            quene.Schedule(t2); // 700 7
            quene.Schedule(t1); // 500 8

            System.Threading.Thread.Sleep(10000);

            Assert.AreEqual(5, count);
            Assert.AreEqual("TestTask - 500", StaticList.List[0]);
            Assert.AreEqual("Test1200Task - 1200", StaticList.List[1]);
            Assert.AreEqual("TestTask - 500", StaticList.List[2]);
            Assert.AreEqual("Test700Task - 700", StaticList.List[3]);
            Assert.AreEqual("Test200Task - 200", StaticList.List[4]);
        }
    }
}
